package com.example.administrator.fastdream.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.administrator.fastdream.R;
import com.example.administrator.fastdream.date.bean.JoinRecordPersonBean;
import com.example.administrator.fastdream.date.presenter.JoinRecordPresenter;
import com.example.administrator.fastdream.date.utils.MyUtils;
import com.example.administrator.fastdream.view.base.BaseActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JoinRecordActivity extends BaseActivity implements JoinRecordPresenter.JoinRecordCallBack {

    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private JoinRecordPresenter presenter;
    private String issue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join_record);
        ButterKnife.bind(this);
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setText("参与记录");
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        issue = getIntent().getStringExtra("issue");
        presenter = new JoinRecordPresenter(this, this);
        if (!TextUtils.isEmpty(issue)) {
            tvTime.setText(issue.substring(0, 4) + "年" + issue.substring(4, 6) + "月" + issue.substring(6, 8) + "日第" + issue.substring(8, issue.length()) + "期");
        }
        presenter.getJoinRecord(issue);
    }

    @Override
    public void getJoinRecord(JoinRecordPersonBean personalInfoBean) {
        final List<JoinRecordPersonBean.DataEntity.PerListEntity> per_list = personalInfoBean.getData().getPer_list();
        tvCount.setText(per_list.size() + "人参与");
        JoinRecordAdapter recordAdapter = new JoinRecordAdapter(R.layout.item_join_record, per_list);
        recyclerView.setAdapter(recordAdapter);
        recordAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_dream) {
                    Intent intent = new Intent(mContext, DreamDetailActivity.class);
                    intent.putExtra("uid", per_list.get(position).getUid());
                    startActivity(intent);
                }
            }
        });
    }

    private class JoinRecordAdapter extends BaseQuickAdapter<JoinRecordPersonBean.DataEntity.PerListEntity, BaseViewHolder> {

        public JoinRecordAdapter(int layoutResId, @Nullable List<JoinRecordPersonBean.DataEntity.PerListEntity> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, JoinRecordPersonBean.DataEntity.PerListEntity item) {
            Glide.with(mContext).load(item.getAvatar()).into((ImageView) helper.getView(R.id.iv_head));
            helper.setText(R.id.tv_name, item.getUsername())
                    .setText(R.id.tv_luck_number, item.getLucky_number())
                    .setText(R.id.tv_number, item.getActive_number())
                    .setText(R.id.tv_time, MyUtils.getDateToStringY(item.getPay_time_msec()))
                    .addOnClickListener(R.id.tv_dream);
        }
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
